using AdminAuthority.Attributes;

using Abc.Application;

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

using System.Threading.Tasks;
using Tool;

namespace Abc.Web.Mvc.Areas.HtAdmin.Controllers
{
    [AuthorityController(typeCode: "HtAdmin", controllerDescription: "系统设置")]
    public class SysSettingController : HtAdminBaseController
    {
        private readonly ILogger<UserController> _logger;
        private readonly IDataConfigService _dataConfigService;

        public SysSettingController(ILogger<UserController> logger
        , IDataConfigService dataConfigService)
        {
            _logger = logger;
            _dataConfigService = dataConfigService;
        }

        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 系统设置
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> SystemSetting()
        {
            var model = await _dataConfigService.GetSetting<SystemSettingDto>("SystemSetting");
            return View(model);
        }

        [HttpPost]
        public async Task<JsonResult> SystemSetting(SystemSettingDto input)
        {
            var resultdata = await _dataConfigService.AddOrUpdateSetting<SystemSettingDto>(input, Dpid: "SystemSetting");
            return Json(resultdata);
        }

        /// <summary>
        /// 邮件设置
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> EmailSetting()
        {
            var model = await _dataConfigService.GetSetting<EmailSettingDto>("EmailSetting");
            return View(model);
        }

        [HttpPost]
        public async Task<JsonResult> EmailSetting(EmailSettingDto input)
        {
            var resultdata = await _dataConfigService.AddOrUpdateSetting<EmailSettingDto>(input, Dpid: "EmailSetting");
            return Json(resultdata);
        }

        [HttpPost]
        public IActionResult ImgUpload(string type)
        {
            var uploadresult = WebHelper.FileUpload(typeName: type);
            return Json(uploadresult);
            //return Json(new { code = 1, msg = uploadresult.Errors, data = uploadresult.Data });
        }
        [HttpPost]
        public IActionResult FileUpload(string type)
        {
            var uploadresult = WebHelper.FileUpload(typeName: type);
            return Json(uploadresult);
            //return Json(new { code = 1, msg = uploadresult.Errors, data = uploadresult.Data });
        }
    }
}